﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using System.IO;

public partial class UserControls_Register : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_Register_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            if (User.GetUserByUserName(TextBox_UserName.Text.Trim()) != -1)
            {
                Label_Result.Text = "Tài khoản đã có";
                return;
            }
            if (User.GetUserbyEmail(TextBox_Email.Text.Trim()) != -1)
            {
                Label_Result.Text = "Email đã được dùng";
                return;
            }
            User user = new User();
            user.UserName = TextBox_UserName.Text.Trim();
            user.Password = TextBox_Password.Text;
            user.FirstName = TextBox_FirstName.Text.Trim();
            user.LastName = TextBox_LastName.Text.Trim();
            user.Email = TextBox_Email.Text.Trim();
            user.Avatar = "";
            user.Active = 0;
            user.Online = false;
            user.CreateDate = DateTime.Now;
            if (FileUpload_Avatar.HasFile)
            {
                string strFileName = Path.GetFileName(FileUpload_Avatar.FileName);
                FileInfo newFileInfo = new FileInfo(strFileName);
                string strExtension = newFileInfo.Extension;
                FileUpload_Avatar.SaveAs(Server.MapPath("~/Files/Avatars/" + user.UserName + newFileInfo.Extension));
                user.Avatar = user.UserName + newFileInfo.Extension;
            }
            User.Insert(user);
        }
    }
}
